﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using MM.DBUtility;
using MM.Common;

namespace WebMMS.Manager
{
    public partial class Browser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["CurUser"] == null)
                {
                    Response.Redirect("~/Login.aspx");
                    return;
                }
                if (!IsPostBack)
                {
                    AspNetPager1.CurrentPageIndex = 1;
                    GVDataBind();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                AspNetPager1.CurrentPageIndex = 1;
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected void btnADD_Click(object sender, EventArgs e)
        {
            try
            {
                string strTitle = txtTitle.Text.Trim();
                if (string.IsNullOrEmpty(strTitle))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "标注不能为空！");
                    return;
                }
                string strMsg = txtMsg.Text.Trim();
                if (string.IsNullOrEmpty(strMsg))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "打开浏览器的url不能为空！");
                    return;
                }
                string strSQL = "INSERT INTO T_Task(Type,Name,Value) VALUES(5,@Name,@Value)";
                List<SqlParameter> parameters = new List<SqlParameter>();
                parameters.Add(SqlHelper.CreateInParameter("@Name", SqlDbType.NVarChar, 500, strTitle));
                parameters.Add(SqlHelper.CreateInParameter("@Value", SqlDbType.NVarChar, 4000, strMsg));
                MM.DBUtility.DbHelperSql.ExecuteSql(strSQL, parameters.ToArray());
                MM.Common.Common.PageStartAlert(this.Page, "添加成功！");
                AspNetPager1.CurrentPageIndex = 1;
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void GVDataBind()
        {
            string strSQL = "SELECT * FROM T_Task a WITH(NOLOCK) WHERE Type=5";
            if (!string.IsNullOrEmpty(txtTitle.Text.Trim()))
            {
                strSQL += string.Format(" AND a.Name LIKE '%{0}%'", txtTitle.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtMsg.Text.Trim()))
            {
                strSQL += string.Format(" AND a.Value LIKE N'%{0}%'", txtMsg.Text.Trim());
            }

            string strOrder = " ORDER BY a.CreateTime DESC";

            AspNetPager1.RecordCount = DbHelperSql.GetSingle("SELECT COUNT(1) FROM (" + strSQL + ") AS B").ToIntOr();

            if (AspNetPager1.CurrentPageIndex == 1)
                strSQL = string.Format(strSQL.Replace("SELECT *", "SELECT TOP {0} * "), AspNetPager1.PageSize) + strOrder;
            else
                strSQL = string.Format("SELECT TOP {0} * FROM ({1}) AS F WHERE rn BETWEEN {2} AND {3}", AspNetPager1.PageSize, strSQL.Replace("SELECT *", string.Format("SELECT *,row_number() over({0}) rn ", strOrder)), (AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex * AspNetPager1.PageSize - 1);

            DataSet ds = MM.DBUtility.DbHelperSql.Query(strSQL);
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
            ViewState["DataTemp"] = ds;
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            GVDataBind();
        }
        protected void GVinfo_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                GVinfo.EditIndex = e.NewEditIndex;
                GVinfo.DataSource = (DataSet)ViewState["DataTemp"];
                GVinfo.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected void GVinfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                GVinfo.EditIndex = -1;
                GVinfo.DataSource = (DataSet)ViewState["DataTemp"];
                GVinfo.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected void GVinfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                string strID = ((HiddenField)GVinfo.Rows[e.RowIndex].FindControl("HF2")).Value;
                DbHelperSql.ExecuteSql(string.Format("DELETE FROM T_Task WHERE ID={0}", strID));
                MM.Common.Common.PageStartAlert(this.Page, "删除成功！");
                AspNetPager1.CurrentPageIndex = 1;
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected void GVinfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                string strID = ((HiddenField)GVinfo.Rows[e.RowIndex].FindControl("HF1")).Value;

                string strTitle = ((TextBox)GVinfo.Rows[e.RowIndex].FindControl("txtTitle")).Text.Trim();
                if (string.IsNullOrEmpty(strTitle))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "标注不能为空！");
                    return;
                }
                string strMsg = ((TextBox)GVinfo.Rows[e.RowIndex].FindControl("txtMsg")).Text.Trim();
                if (string.IsNullOrEmpty(strMsg))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "打开浏览器的url不能为空！");
                    return;
                }

                MM.DBUtility.DbHelperSql.ExecuteSql(string.Format("UPDATE T_Task SET Name=N'{0}',Value=N'{1}' WHERE ID={2}", strTitle, strMsg, strID));
                MM.Common.Common.PageStartAlert(this.Page, "更新成功！");
                GVinfo.EditIndex = -1;
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}